package com.iss.supermarket.TradeUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import com.iss.supermarket.util.MessageWindow.MsgWin;
import com.iss.supermarket.util.entity.SpUser;
import com.iss.supermarket.util.network.NetWork;
import com.iss.supermarket.util.entity.Detailed;
import com.iss.supermarket.util.entity.Goods;
import com.iss.supermarket.util.entity.TradeInfo;
import com.iss.supermarket.util.repository.TradeDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * 成员变量
 * 构造器
 * 初始化函数
 * 内部类
 * 成员方法
 */
public class TradeUI extends JFrame {
    //单例模式
    private static TradeUI tradeUI;
    //定义组件
    private JLabel label1,label2,label3,label4;
    private JLabel[] tradelabels;
    private JLabel[] flowLabels;
    private JTextField[] tradetextFields;
    private JTextField[] flowtext;
    private JButton button1,button2;
    private TradeList tradelist;
    private JPanel TradePanel;
    private String uuid;
    private String modtime;
    private static SpUser thisUser;

    //连接数据库池
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:/spring_ioc.xml");
    TradeDao tradeDao = ctx.getBean("TradeDao", TradeDao.class);

    public TradeUI(){
//        super("交易管理");

        //和扫码应用连接
        NetWork.getNetWork(TradeUI.class.getSimpleName());
        InitAssembly();
        InitUI();
    }

    private void InitUI(){
        JPanel leftPanel = new JPanel();
        JPanel rightPanel = new JPanel();
        Box verticalBox1 = Box.createVerticalBox();
        Box verticalBox2 = Box.createVerticalBox();
        Box HorizontalBox1 = Box.createHorizontalBox();
//        rightPanel.setPreferredSize(new Dimension(400,600));
//        Container container = getContentPane();

        JPanel jPanel1 = new JPanel();
        jPanel1.add(label1);
        verticalBox1.add(jPanel1);

        JPanel jPanel2 = new JPanel();
        jPanel2.add(flowLabels[0]);
        jPanel2.add(flowtext[0]);
        verticalBox1.add(jPanel2);

        JPanel jPanel3 = new JPanel();
        jPanel3.add(flowLabels[1]);
        jPanel3.add(flowtext[1]);
        verticalBox1.add(jPanel3);

        JPanel jPanel4 = new JPanel();
        jPanel4.add(flowLabels[2]);
        jPanel4.add(flowtext[2]);
        verticalBox1.add(jPanel4);

        JPanel jPanel5 = new JPanel();
        jPanel5.add(button1);
        verticalBox1.add(jPanel5);

        JPanel jPanel6 = new JPanel();
        jPanel6.add(label2);
        verticalBox1.add(jPanel6);

        JPanel jPanel7 = new JPanel();
        jPanel7.add(tradelabels[0]);
        jPanel7.add(tradetextFields[0]);
        verticalBox1.add(jPanel7);

        JPanel jPanel8 = new JPanel();
        jPanel8.add(tradelabels[1]);

        jPanel8.add(tradetextFields[1]);
        verticalBox1.add(jPanel8);

        JPanel jPanel9 = new JPanel();
        jPanel9.add(tradelabels[2]);
        jPanel9.add(tradetextFields[2]);
        verticalBox1.add(jPanel9);

        JPanel jPanel10 = new JPanel();
        jPanel10.add(tradelabels[3]);
        jPanel10.add(tradetextFields[3]);
        verticalBox1.add(jPanel10);

        JPanel jPanel11 = new JPanel();
        jPanel11.add(tradelabels[3]);
        jPanel11.add(tradetextFields[3]);
        jPanel11.add(label3);
        verticalBox1.add(jPanel11);

        JPanel jPanel12 = new JPanel();
        jPanel12.add(tradelabels[4]);
        jPanel12.add(tradetextFields[4]);
        verticalBox1.add(jPanel12);

        //交易按钮
        JPanel jPanel13 = new JPanel();
        jPanel13.add(button2);
        verticalBox1.add(jPanel13);
        leftPanel.add(verticalBox1);

        uuid = getUUID();
        modtime = getModtime();
        tradetextFields[0].setText(uuid);
        tradetextFields[4].setText(getModtime());

        JPanel jPanel14 = new JPanel();
        jPanel14.add(label4);
        verticalBox2.add(jPanel14);

        verticalBox2.add(tradelist.getPanelPage());

        rightPanel.add(verticalBox2);
        HorizontalBox1.add(leftPanel);
        HorizontalBox1.add(rightPanel);
        TradePanel.add(HorizontalBox1);

//        container.add(TradePanel);
//        setSize(800,700);
//        setVisible(true);
    }

    private void InitAssembly(){
        label1 = new JLabel("出库信息");
        label1.setFont(new Font("宋体",Font.BOLD,20));
        label2 = new JLabel("交易信息");
        label2.setFont(new Font("宋体",Font.BOLD,20));
        label3 = new JLabel("元");
        label3.setFont(new Font("黑体",Font.PLAIN,15));
        label4 = new JLabel("交易列表");
        label4.setFont(new Font("宋体",Font.BOLD,20));

        label1.setHorizontalAlignment(SwingConstants.CENTER);
        label1.setPreferredSize(new Dimension(210,20));
        label2.setHorizontalAlignment(SwingConstants.CENTER);
        label2.setPreferredSize(new Dimension(210,20));
        label3.setHorizontalAlignment(SwingConstants.RIGHT);
        label3.setPreferredSize(new Dimension(15,20));
        label4.setHorizontalAlignment(SwingConstants.CENTER);
        label4.setPreferredSize(new Dimension(210,20));

        flowLabels = new JLabel[3];
        flowLabels[0] = new JLabel("条形码：");
        flowLabels[1] = new JLabel("数量：");
        flowLabels[2] = new JLabel("商品名称：");
        for(int i=0;i<flowLabels.length;++i){
            flowLabels[i].setHorizontalAlignment(SwingConstants.LEFT);
            flowLabels[i].setPreferredSize(new Dimension(100, 20));
            flowLabels[i].setFont(new Font("黑体",Font.PLAIN,15));
        }

        flowtext = new JTextField[3];
        for(int i=0;i<flowtext.length;++i){
            flowtext[i] = new JTextField();
            flowtext[i].setHorizontalAlignment(SwingConstants.CENTER);
            flowtext[i].setPreferredSize(new Dimension(210,25));
            if(i == 2)
                flowtext[i].setEnabled(false);
            flowtext[i].setFont(new Font("黑体",Font.PLAIN,15));
        }

        tradelabels = new JLabel[5];
        tradelabels[0] = new JLabel("交易编码：");
        tradelabels[1] = new JLabel("收银员：");
        tradelabels[2] = new JLabel("总数量：");
        tradelabels[3] = new JLabel("总金额：");
        tradelabels[4] = new JLabel("交易信息：");
        for(int i=0;i<tradelabels.length;++i){
            tradelabels[i].setHorizontalAlignment(SwingConstants.LEFT);
            tradelabels[i].setPreferredSize(new Dimension(100, 20));
            tradelabels[i].setFont(new Font("黑体",Font.PLAIN,15));
        }

        tradetextFields = new JTextField[5];
        for(int i=0;i<tradetextFields.length;++i){
            tradetextFields[i] = new JTextField();
            tradetextFields[i].setHorizontalAlignment(SwingConstants.CENTER);
            if(i == 3){
                tradetextFields[i].setPreferredSize(new Dimension(190,25));
            }else{
                tradetextFields[i].setPreferredSize(new Dimension(210,25));
            }
            tradetextFields[i].setFont(new Font("黑体",Font.PLAIN,13));
        }

        tradetextFields[0].setText(uuid);
        tradetextFields[0].setEnabled(false);
        tradetextFields[1].setText(thisUser.getNiceName());
        tradetextFields[1].setEnabled(false);
        tradetextFields[4].setText(modtime);
        tradetextFields[4].setEnabled(false);

        //测试
        flowtext[0].setText("6923450656198");
        flowtext[1].setText("1");

        button1 = new JButton("出库");
        button1.setHorizontalAlignment(SwingConstants.CENTER);
        button1.setPreferredSize(new Dimension(80,20));
        button1.setFont(new Font("黑体",Font.PLAIN,14));
        button1.addActionListener(new ButtonAction());
        button2 = new JButton("交易");
        button2.setHorizontalAlignment(SwingConstants.CENTER);
        button2.setPreferredSize(new Dimension(80,20));
        button2.setFont(new Font("黑体",Font.PLAIN,14));
        button2.addActionListener(new ButtonAction());

        tradelist = new TradeList();
        TradePanel = new JPanel();
        TradePanel.setPreferredSize(new Dimension(800,700));
    }

    public JPanel getTradePanel() {
        return TradePanel;
    }

    private class ButtonAction implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            //TradeInfo类用于更新sp_trade_info
            TradeInfo tradeInfo = new TradeInfo();
            if("交易".equals(e.getActionCommand())){
                //更新sp_detailed
                for(int i=0;i<tradelist.getListLength();++i){
                    Detailed tmpdetailed = tradelist.getListModelArray().getElementAt(i);
                    tradeDao.InsertDetailed(tmpdetailed.getGdBarCode(),
                            tmpdetailed.getTrUuid(),
                            tmpdetailed.getGdBroadName(),
                            tmpdetailed.getDtgdNum(),
                            tmpdetailed.getDtgdPrice(),
                            tmpdetailed.getModetime());
                }

                tradeInfo.setTrUuid(uuid);
                tradeInfo.settrTotalNum(tradelist.getTotalNum());
                tradeInfo.setNiceName(tradetextFields[1].getText());
                tradeInfo.setTrTotalAmount(String.valueOf(tradelist.getTotalAmount()));
                tradeInfo.setModetime(modtime);

                //更新sp_trade_info
                tradeDao.InsertTradeInfo(tradeInfo.getTrUuid(),
                        tradeInfo.getNiceName(),
                        tradeInfo.gettrTotalNum(),
                        tradeInfo.getTrTotalAmount(),
                        tradeInfo.getModetime());

                Reset();
                tradelist.Clear();

                //测试
//                flowtext[0].setText("6923450656198");
//                flowtext[1].setText("1");

                MsgWin msgWin = new MsgWin("交易成功");
            }else if("出库".equals(e.getActionCommand())){
                Goods good = Query();
                flowtext[2].setText(good.getGdBroadName());

                //检查商品的出库数量，创建detailed类，刷新商品交易列表
                if(Integer.parseInt(flowtext[1].getText()) == 0){
                    MsgWin msgWin = new MsgWin("商品数量为0");
                }else{
                    Detailed detailed = new Detailed(flowtext[0].getText(),uuid,flowtext[2].getText(),Integer.parseInt(flowtext[1].getText()),good.getGdPrice(),modtime);
                    tradelist.flushList(detailed);
                }

                //计算并显示商品总数量和商品总价
                tradetextFields[2].setText(String.valueOf(tradelist.getTotalNum()));
                tradetextFields[3].setText(String.valueOf(tradelist.getTotalAmount()));
            }
        }
    }

    public Goods Query(){
        //根据条形码查询商品信息
        Goods good = tradeDao.queryGoodsInfo(flowtext[0].getText());
        if(good == null){
            MsgWin msgWin = new MsgWin("不存在的商品信息");
        }
        return good;
    }

    public void setScanningResults(String barcode){
        //接收扫码结果，更新信息
        flowtext[0].setText("");
        flowtext[0].setText(barcode);
        Goods good = tradeDao.queryGoodsInfo(flowtext[0].getText());
        System.out.println(flowtext[0].getText());
        if(good != null){
            flowtext[1].setText("1");
            flowtext[2].setText(good.getGdBroadName());
            Detailed detailed = new Detailed(flowtext[0].getText(),uuid,flowtext[2].getText(),Integer.parseInt(flowtext[1].getText()),good.getGdPrice(),modtime);
            tradelist.flushList(detailed);
        }else{
            MsgWin msgWin = new MsgWin("不存在的商品信息");
        }
    }

    public String getModtime(){
        Date date=new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public String getUUID(){
        return UUID.randomUUID().toString();
    }

    public void Reset(){
        //在交易完成后用于刷新交易信息
        for(int i=0;i<flowtext.length;++i){
            flowtext[i].setText("");
        }
        for(int i=0;i<tradetextFields.length;++i){
            tradetextFields[i].setText("");
        }

        uuid = getUUID();
        modtime = getModtime();

        tradetextFields[0].setText(uuid);
        tradetextFields[1].setText(thisUser.getNiceName());
        tradetextFields[4].setText(modtime);
    }

    public static void setThisUser(SpUser spUser){
        thisUser = spUser;
    }

    public static synchronized TradeUI getTradeInfoUI(){
        if(tradeUI == null)
            tradeUI = new TradeUI();
        return tradeUI;
    }
}